package edu.gatech.geochat.model;

import android.location.Location;
import android.location.LocationManager;

public class GeoMessage {
	
	private String anon;
	private String id;
	private int upvotes = 0, downvotes = 0;
	private String user, email, text, location, password, range;
	private Location loc;
	
	private static String n(String s) {
		return n(s, "");
	}
	private static String n(String s, String d) {
		return s == null ? d : s;
	}
	
	public GeoMessage(String user, String password, String email, String text, String location, String range) {
		this.user 	  = n(user);
		this.password = n(password);
		this.email    = n(email);
		this.text 	  = n(text);
		this.location = n(location, "0,0");
		this.range 	  = n(range, "0");
		
		String[] coord=location.split(",");
		
		double lat=Double.parseDouble(coord[1]);
		double longt=Double.parseDouble(coord[1]);
		loc=new Location(LocationManager.GPS_PROVIDER);
		loc.setLatitude(lat);
		loc.setLongitude(longt);
	}
	
	public String getAnon() { return anon; }
	public void setAnon(String anon) { this.anon = anon; }
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	public int getUpvotes() {
		return upvotes;
	}
	
	public int getDownvotes() {
		return downvotes;
	}
	
	public int getVotes() {
		return upvotes - downvotes;
	}

	public void setUpvotes(String up) {
		if (up != null && up.matches("\\d+"))
			upvotes = Integer.parseInt(up);
	}
	public void setDownvotes(String down) {
		if (down != null && down.matches("\\d+"))
			downvotes = Integer.parseInt(down);
	}
	
	public void upvote() {
		upvotes++;
	}
	public void downvote() {
		downvotes++;
	}
	
	public String getUser() {
		return user;
	}

	public String getEmail() {
		return email;
	}

	public String getText() {
		return text;
	}

	public String getLocation() {
		return location;
	}
	
	public String getPassword() {
		return password;
	}
	
	public String getRange(){
		return range;
	}
	
	public Location getLoc(){
		return loc;
	}
	
}
